package com.glorystone.ins.interceptor;
import com.glorystone.ins.utils.LogUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LogInterceptor implements HandlerInterceptor{

    /**
     * 可以考虑作权限，日志，事务等等
     * 该方法在目标方法调用之前被调用；
     * 若返回TURE,则继续调用后续的拦截器和目标方法
     * 若返回FALSE,则不会调用后续的拦截器和目标方法
     *
     */
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object obj) throws Exception {
        return true;
    }
    /**
     * 该方法在目标方法调用之后，渲染视图之前被调用；
     * 可以对请求域中的属性或视图做出修改
     *
     */
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    }
    /**
     * 在渲染视图之后被调用；
     * 可以用来释放资源
     */
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        LogUtils.saveLog(httpServletRequest,o,e,null);
    }
}
